import {getGoods} from '@/api/goods'

const actionTypes = {
  SET_DATA: '@setData',
};

const home = {
  state: {
    categories: [
      {id: '1', name: '香水', subCategories: [{id: '11', name: 'Combined'}, {id: '12', name: 'Dior Homme'}]},
      {id: '2', name: '彩妆', subCategories: [{id: '21', name: 'Addict Make-up'}, {id: '22', name: 'BackStage'}]},
      {id: '3', name: '护肤', subCategories: [{id: '31', name: 'Capture Totale'}, {id: '32', name: 'Cleanser'}]},
    ],
    filter: {
      category: '',
      subCategory: '',
      keyword: '',
    },
    sort: {
      sortField: 'price',
      sortType: 'ASC',
    },
    data: {
      pager: {current: 1, pageSize: 20, total: 0},
      dataList: [],
    }
  },
  mutations: {
    SET_DATA: (state, data) => {
      state.data.pager = data.pager;
      state.data.dataList = data.dataList;
      state.sort = data.sort;
      state.filter = data.filter;
    },
    SET_CATEGORY: (state, data) => {
      state.filter.category = data.id;
    },
    SET_SUB_CATEGORY: (state, data) => {
      state.filter.subCategory = data.id;
    },
  },
  actions: {
    clickCategory({commit}, payload){
      commit('SET_CATEGORY', payload);
    },
    clickSubCategory({commit}, payload){
      commit('SET_CATEGORY', payload.category);
      commit('SET_SUB_CATEGORY', payload.subCategory);
    },
    HomePageByFilter({commit}, payload){
      const {filter, pager, sort}=payload;
      return new Promise((resolve, reject) => {
        getGoods(filter, pager, sort).then(response => {
          const data = response.data;
          commit('SET_DATA', data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }
  },
};

export default home;
